package cn.zzszxyy.hr.repository;

import cn.zzszxyy.hr.model.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-26
 * 说明:用户管理
 */
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Integer>, JpaSpecificationExecutor<UserEntity> {

    @Query("select distinct s.userName from UserEntity s")
    List<String> getAllUserName();

    Page<UserEntity> findByUserNameLike(String userName, Pageable pageable);

    List<UserEntity> findByLoginNameAndUserPwd(String loginName, String userPwd);

    List<UserEntity> findByLoginName(String loginName);

    Page<UserEntity> findByUserName(String userName, Pageable pageable);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("update UserEntity u set u.userPwd =?1 where u.userId = ?2")
    int restUserPwdFor(String userPwd, int userId);

    @Modifying(clearAutomatically = true)
    @Transactional
    @Query("update UserEntity u set u.userPwd =?1 where u.userId = ?2")
    int changeUserPwdFor(String userPwd, int userId);
}
